************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates main paper, Table 1 (Sleep Statistics in Two Samples in Chennai)
* Last edited: 07 May 2021
************************************************

* PANEL A - RCT SAMPLE
use "$d/firststage_dataset.dta", clear

*Generate dummies for sleep less than X hours
gen sleep_less_7 = sleep_night<=7
gen sleep_less_6 = sleep_night<=6

distinct pid if !mi(sleep_night)
local rct_pid_n = `r(ndistinct)'

count if post_treatment == 0 & !mi(sleep_night)
local rct_obs_n = `r(N)'

keep if post_treatment == 0 // use baseline data
keep if sleep_night!=.

* Percent below 6 &  7 hours (daily level)
foreach var in sleep_less_6 sleep_less_7 {
	sum `var'
	local `var'_m_day = string(r(mean),"%9.2f")
	local `var'_sd_day = string(r(sd),"%9.2f")	
}		

cap drop n_awake_rct
gen n_awake_rct = an_12_number_of_awakenings

* Collapsing to get average nap duration, sleep eff, sleep hours, and 24-hour sleep
collapse (mean) sleep_night sleep_eff act_inbed self_report n_awake_rct, by(pid)
gen sleep_less_7 = sleep_night<=7
gen sleep_less_6 = sleep_night<=6

	foreach var in sleep_night sleep_eff act_inbed self_report n_awake_rct sleep_less_6 sleep_less_7 {
		sum `var'
		local `var'_m = string(r(mean),"%9.2f")
		local `var'_sd = string(r(sd),"%9.2f")
	}		
	

* PANEL B - POPULATION SAMPLE
use "$d/pop_study_dataset.dta", clear

keep if acti_sleep!=.

distinct pid if !mi(acti_sleep)
local pid_n_pop = `r(ndistinct)'

count if !mi(acti_sleep)
local obs_n_pop `r(N)'

*Generate dummies for sleep less than X hours
gen sleep_less_7 = acti_sleep<=7
gen sleep_less_6 = acti_sleep<=6

* Generate actigraph time in bed measure for population study
gen acti_time_bed_ps = acti_sleep/acti_sleep_eff

* Percent napping on a given day (daily-level)
sum acti_whether_nap_day

local acti_whether_nap_m_pop = string(r(mean),"%9.2f")
local acti_whether_nap_sd_pop = string(r(sd),"%9.2f")

* Percent below 6 &  7 hours (daily level)

foreach var in sleep_less_6 sleep_less_7 {
	sum `var'
	local `var'_m_day_pop = string(r(mean),"%9.2f")
	local `var'_sd_day_pop = string(r(sd),"%9.2f")
}		

* Collapsing to get average nap duration, sleep eff, sleep hours, and 24-hour sleep
collapse (mean) acti_avg_nap acti_sleep_eff acti_sleep total_sleep acti_time_bed_ps sr_sleep, by(pid)

	*Generate dummies for sleep less than X hours
	gen sleep_less_7 = acti_sleep<=7
	gen sleep_less_6 = acti_sleep<=6

	foreach var in acti_avg_nap acti_sleep_eff acti_sleep total_sleep acti_time_bed_ps sr_sleep sleep_less_7 sleep_less_6 {
		qui sum `var'
		local `var'_m_pop = string(r(mean),"%9.2f")
		local `var'_sd_pop = string(r(sd),"%9.2f")
	}	
	
*Outputting table to tex file
cd "$om/Tables"
	file open f using "Table1_sleep_statistics.tex", write replace
		file write f "\resizebox{1\textwidth}{!}{ " _n ///
		"\begin{tabular*}{\textwidth}{l@{\extracolsep{\fill}}*{2}{c}}" _n ///
		"\toprule" _n ///
		"\addlinespace \\" _n ///
		///
		" & \textbf{RCT Sample} & \textbf{Broader Sample} \\" _n ///
		" & \textbf{(pre-treatment)} & \\" _n ///
		" & (1) &(2)  \\" _n ///
		"\midrule" _n ///
		"\textbf{Night Sleep} & & \\" _n ///
		"Hours in bed & `act_inbed_m' (`act_inbed_sd') & `acti_time_bed_ps_m_pop' (`acti_time_bed_ps_sd_pop') \\" _n ///
		"Hours asleep  & `sleep_night_m' (`sleep_night_sd') & `acti_sleep_m_pop' (`acti_sleep_sd_pop') \\" _n ///		
		"Sleep efficiency  & `sleep_eff_m' (`sleep_eff_sd') & `acti_sleep_eff_m_pop' (`acti_sleep_eff_sd_pop') \\" _n ///	
		"Number of Awakenings & `n_awake_rct_m' (`n_awake_rct_sd')  & 27.4 (10.14) \\" _n ///	
		"Fraction sleeping below 7 hours  & & \\" _n ///	
		"\ \ \ Participant-level  & `sleep_less_7_m' (`sleep_less_7_sd') & `sleep_less_7_m_pop' (`sleep_less_7_sd_pop') \\" _n ///	
		"\ \ \ Participant-day-level  & `sleep_less_7_m_day' (`sleep_less_7_sd_day') & `sleep_less_7_m_day_pop' (`sleep_less_7_sd_day_pop') \\" _n ///	
		"Fraction sleeping below 6 hours  & &  \\" _n ///		
		"\ \ \ Participant-level  & `sleep_less_6_m' (`sleep_less_6_sd') & `sleep_less_6_m_pop' (`sleep_less_6_sd_pop') \\" _n ///	
		"\ \ \ Participant-day-level  & `sleep_less_6_m_day' (`sleep_less_6_sd_day') & `sleep_less_6_m_day_pop' (`sleep_less_6_sd_day_pop') \\" _n ///
		"Self-reported hours asleep & `self_report_m' (`self_report_sd') & `sr_sleep_m_pop' (`sr_sleep_sd_pop') \\" _n ///
		"\addlinespace \\" _n ///
		"\textbf{Nap sleep} & & \\" _n ///		
		"Percent napping on a given day & N/A & `acti_whether_nap_m_pop' (`acti_whether_nap_sd_pop') \\" _n ///		
		"Duration of naps (conditional on napping) & N/A & `acti_avg_nap_m_pop' (`acti_avg_nap_sd_pop') \\" _n ///
		"\addlinespace \\" _n ///
		"\textbf{Total sleep} & & \\" _n ///	
		"Hours asleep & `sleep_night_m' (`sleep_night_sd') & `total_sleep_m_pop' (`total_sleep_sd_pop') \\" _n ///	
		"\midrule" _n ///
		"Participant-nights & `rct_obs_n' & `obs_n_pop' \\" _n ///
		"Participants & `rct_pid_n' & `pid_n_pop' \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular*}" _n ///
		"}" _n 
file close f		
